package com.csy.crud.dao.impl;

import com.csy.crud.dao.HandlerDao;
import com.csy.crud.entity.Staff;
import com.csy.crud.util.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.*;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * @author CSY
 * @date 2025 - 02 - 27
 */
public class HandlerDaoImpl implements HandlerDao {
    QueryRunner qr = new QueryRunner(DruidUtil.getDataSource());
    @Override
    public Map<String, Object> selectByIdToMap(int id) {
        String sql = "select id,name,age,phone,sta_pos staPos,add_time addTime,update_time updateTime from staffs where id = ?";

        try {
            return qr.query(sql,new MapHandler(),id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Map<String, Object>> selectAllToMap() {
        String sql = "select id,name,age,phone,sta_pos staPos,add_time addTime,update_time updateTime from staffs";

        try {
            return qr.query(sql,new MapListHandler());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Staff selectById(int id) {
        String sql = "select id,name,age,phone,sta_pos staPos,add_time addTime,update_time updateTime from staffs where id = ?";

        try {
            return qr.query(sql,new BeanHandler<>(Staff.class),id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Staff> selectAll() {
        String sql = "select id,name,age,phone,sta_pos staPos,add_time addTime,update_time updateTime from staffs";

        try {
            return qr.query(sql,new BeanListHandler<>(Staff.class));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String selectNameById(int id) {
        String sql = "select name from staffs where id = ?";

        try {
            return qr.query(sql,new ScalarHandler<>(),id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Long selectCount() {
        String sql = "select count(*) from staffs";

        try {
            return qr.query(sql,new ScalarHandler<>());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<String> selectAllName() {
        String sql = "select name from staffs";
        try {
            return qr.query(sql,new ColumnListHandler<>());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Object[] selectByIdToArray(int id) {
        String sql = "select id,name,age,phone,sta_pos staPos,add_time addTime,update_time updateTime from staffs where id = ?";

        try {
            return qr.query(sql,new ArrayHandler(),id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Object[]> selectAllToArray() {
        String sql = "select id,name,age,phone,sta_pos staPos,add_time addTime,update_time updateTime from staffs";

        try {
            return qr.query(sql,new ArrayListHandler());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


}
